const { tipToast, reLaunchTo } = require('../../common/platform');
const { getNoticeTamplateId } = require('../../api/shop');
const { PAGE_NAV } = require('../../constants/route');
const { PageBase } = require('../../mixins/page');

Page({
    mixins: [PageBase],
    data: {
        tplList: [''],
    },

    async onShow() {
        await this.getNoticeTemplateId();
    },
    // 获取添加的提醒id
    async getNoticeTemplateId() {
        const list = await getNoticeTamplateId();
        this.setData({
            tplList: list,
        });
    },
    // 判断是否全部同意
    isAllAccept: function(object) {
        let isAll = true;
        for (let key in object) {
            if (key !== 'errMsg' && object[key] !== 'accept') {
                isAll = false;
            }
        }
        return isAll;
    },
    // 添加订阅消息
    addNotice: function() {
        const _this = this;
        const { tplList } = this.data;
        tplList.length > 0 && wx.requestSubscribeMessage({
            tmplIds: tplList,
            success: function(res) {
                console.log('添加订阅成功 => ', res);
                const isAll = _this.isAllAccept(res);
                isAll && tipToast('添加提醒成功');
            },
            fail: function(err) {
                console.log('添加订阅失败 => ', err);
                tipToast('添加提醒失败，请重试');
            },
        });
    },
     // 查看订单详情
     gotoOrderList: function() {
        reLaunchTo(PAGE_NAV.orderList);
    },
})